package com.diyunapp.xilinapp.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AboutUsActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back1)
    ImageView goBack1;
    @Bind(R.id.logo)
    ImageView logo;
    @Bind(R.id.company_introduce)
    WebView companyIntroduce;
    @Bind(R.id.company_link)
    WebView company_link;
    @Bind(R.id.company_web)
    TextView companyWeb;
    @Bind(R.id.company_email)
    TextView companyEmail;
    @Bind(R.id.company_qq)
    TextView companyQq;
    private View headView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        headView = findViewById(R.id.aboutus_menubar_inc);
        super.MenuWidget(headView);
        cancel();
        link();
    }

    private void link() {
        String url=""+ MainActivity.url+"act=goods&op=about&article_id=67";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                                toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject information = resultObject.optJSONObject("data");
                                Log.i("zhangxiaoxidebug", "information=" + information);
                                String article_content=information.optString("article_content");
                                String ac_id=information.optString("ac_id");
                                company_link.getSettings().setDefaultTextEncodingName("UTF-8") ;
                                company_link.loadDataWithBaseURL(null, article_content, "text/html","UTF-8", null);
////                                toast(article_content);
////                                Log.i("zhangxiaoxidebug", "article_content=" + article_content);
//                                String[] strarray1 = article_content.split("");
//                                String b = strarray1[strarray1.length -1].toString();
////                                toast(b);
//                                Log.i("STRSTRSTRSTRSTR", "==========" + b);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {

                        toast("请求失败");

                    }
                });
    }

    public void cancel() {
        String url=""+MainActivity.url+"act=goods&op=about&article_id=54";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                                toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject information = resultObject.optJSONObject("data");
                                Log.i("zhangxiaoxidebug", "information=" + information);
                                String article_content=information.optString("article_content");
                                String ac_id=information.optString("ac_id");
                                companyIntroduce.getSettings().setDefaultTextEncodingName("UTF-8") ;
                                companyIntroduce.loadDataWithBaseURL(null, article_content, "text/html","UTF-8", null);
////                                toast(article_content);
////                                Log.i("zhangxiaoxidebug", "article_content=" + article_content);
//                                String[] strarray1 = article_content.split("");
//                                String b = strarray1[strarray1.length -1].toString();
////                                toast(b);
//                                Log.i("STRSTRSTRSTRSTR", "==========" + b);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {

                        toast("请求失败");

                    }
                });
    }

    @OnClick( R.id.go_back1)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back1:
//                nextAct(SettingsActivity.class);
                finish();
                break;


        }
    }
    public void toast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    public void nextAct(Class act) {
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }
}
